Table of Contents Previous Section

Accessing and Sharing Variables

WebScript automates the process of accessing scoped variables, whether they're declared in an application script or in a component script. For a scoped variable myVar, for example, you can set and return its value from the script that declares it as follows:

[self myVar];
[self setMyVar:newValue];

You don't have to implement these methods to invoke them---WebScript does this work behind the scenes. For example, you may notice that the Visitors Application.wos script doesn't implement visitorNum, setVisitorNum:, or setLastVisitor: methods, yet the Main.wos script invokes them. Note that for "set" methods, WebScript capitalizes the first letter of the variable name if it's not already an uppercase letter and prepends the word "set" to it.

In these statements:

[self myVar];
[self setMyVar:newValue];

the myVar and setMyVar: messages are sent to self, which indicates that the scoped variable myVar is declared in the script that's accessing it. Sometimes a component script has to access scoped variables (that is, global or session variables) declared in the application script. When you work with global and session variables, remember that they're owned by the application object WOApplication. To set or return their values, you send a message to the WOApplication object. For example, the Main.wos script in the Visitors example includes these statements:

number = [WOApp visitorNum];
[WOApp setVisitorNum:number];    
[WOApp setLastVisitor:[WOApp aName]];

WOApp refers to the application object. The global variable WOApp is shorthand for the following statement:

[WOApplication sharedInstance];

This statement returns the single WOApplication object that's accessed by all users of an application.

You can also access a scoped variable declared in one script from another script. This is something you commonly do right before you navigate to a new page, for example:

id anotherPage = [WOApp pageWithName:@"Hello"]; 
[anotherPage setNameString:newValue];

The current script uses the statement [anotherPage setNameString:newValue]; to set the value of nameString, which is declared in the page entitled "Hello".

Table of Contents Next Section